package com.jeesite.modules.app.examinate.extheory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.common.mapper.JsonMapper;
import com.jeesite.modules.app.mockexam.theory.record.entity.AppMockexamTheoryRecord;
import com.jeesite.modules.app.util.answer.AnswerUtils;
import com.jeesite.modules.conf.AppController;
import com.jeesite.modules.conf.MessageCode;
import com.jeesite.modules.zx.examination.barbecue.entity.TexaminationBarbecue;
import com.jeesite.modules.zx.examination.barbecue.record.entity.TexaminationBarbecueRecord;
import com.jeesite.modules.zx.examination.barbecue.record.service.TexaminationBarbecueRecordService;
import com.jeesite.modules.zx.examination.barbecue.service.TexaminationBarbecueService;
import com.jeesite.modules.zx.question.bank.entity.TquestionBank;
import com.jeesite.modules.zx.question.bank.service.TquestionBankService;

@Controller
@RequestMapping(value = "/app/examination/barbecue/record/")
public class AppExaminationBarbecueRecordController {
	@Autowired
	private TexaminationBarbecueRecordService texaminationBarbecueRecordService;
	@Autowired
	private TexaminationBarbecueService texaminationBarbecueService;
	@Autowired
	private TquestionBankService tquestionBankService;
	@PostMapping(value = "saveRecord")
	public void saveRecord(TexaminationBarbecueRecord entity,HttpServletRequest request,HttpServletResponse response) {
		String theoryId = entity.getTheoryId();
		if(StringUtils.isBlank(theoryId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"理论标识不存在");
			return;
		}
		String questionId = entity.getQuestionId();
		if(StringUtils.isBlank(questionId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"问题标识不存在");
			return;
		}
		Integer questionType = entity.getQuestionType();
		if(questionType==null){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"问题类型不存在");
			return;
		}
         String userId=entity.getUserId();		// 用户主键
         if(StringUtils.isBlank(userId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "用户标识");
			 return;
		 }
		 String answerId=entity.getAnswerId();		// 答案字段
         if(StringUtils.isBlank(answerId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "答案信息");
			 return;
		 }
         String postId=entity.getPostId();
         if(StringUtils.isBlank(postId)){
        	 AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED, "试卷标识");
			 return;
         }
         TexaminationBarbecueRecord asrmodel=new TexaminationBarbecueRecord();
         asrmodel.setTheoryId(theoryId);
         asrmodel.setQuestionType(questionType);
         asrmodel.setUserId(userId);
         asrmodel.setPostId(postId);
         asrmodel.setQuestionId(questionId);
         List<TexaminationBarbecueRecord> findList = texaminationBarbecueRecordService.findList(asrmodel);
         String id="";
         if(findList!=null&&findList.size()>0){
        	 id=findList.get(0).getId();
         }
         TquestionBank answerObj = tquestionBankService.get(questionId);
   		 String objAnswer = answerObj.getObjAnswer();
   		 if(StringUtils.isNotBlank(objAnswer)) {
   			//先判断长度
   			boolean sco=AnswerUtils.calculation(objAnswer, answerId);
		    if(sco) {
				Double score = answerObj.getScore();
			    entity.setScore(new BigDecimal(score));
			}else {
				entity.setScore(new BigDecimal(0.0));
			}
   		 }
   		 
   		 if(StringUtils.isNotBlank(id)){
   			entity.setId(id);
   			texaminationBarbecueRecordService.update(entity);
   		 }else{
   			texaminationBarbecueRecordService.save(entity);
   		 }
   		 JSONObject obj=JSONObject.parseObject(JsonMapper.toJson(entity));
   		 AppController.renderSuccessObject(response, obj, MessageCode.SUCCESS_ACK);
	}
	@RequestMapping(value ="add")
	public void add(TexaminationBarbecueRecord entity, HttpServletRequest request,HttpServletResponse response){
		String theoryId = entity.getTheoryId();
		if(StringUtils.isBlank(theoryId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"理论标识不存在");
			return;
		}
		String questionId = entity.getQuestionId();
		if(StringUtils.isBlank(questionId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"问题标识不存在");
			return;
		}
		Integer questionType = entity.getQuestionType();
		if(questionType==null){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"问题类型不存在");
			return;
		}
		String userId = entity.getUserId();
		if(StringUtils.isBlank(userId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"用户信息");
			return;
		}
		String answerId = entity.getAnswerId();
		if(StringUtils.isBlank(answerId)){
			AppController.renderErrorString(response, MessageCode.PARAMETER_IS_NEED,"答案信息");
			return;
		}
		TexaminationBarbecueRecord model=new TexaminationBarbecueRecord();
		model.setQuestionId(questionId);
		model.setUserId(userId);
		List<TexaminationBarbecueRecord> findList = texaminationBarbecueRecordService.findList(model);
		if(findList!=null&&findList.size()>0){
			entity.setId(findList.get(0).getId());
		}
		String[] split = answerId.split(",");
		List<String> list=new ArrayList<String>();
		for(String id:split){
			list.add(id);
		}
		TexaminationBarbecue texaminationBarbecue = new TexaminationBarbecue();
		texaminationBarbecue.setParentCode(questionId);
		texaminationBarbecue.setObjAnswer("1");
		List<TexaminationBarbecue> findList2 = texaminationBarbecueService.findList(texaminationBarbecue);
		int a=findList2.size();
		int b=list.size();
		if(a==b){
			boolean addScor=true;
			for(TexaminationBarbecue tb: findList2){
				String id = tb.getId();
				if(!list.contains(id)){
					addScor=false;
				}
			}
			if(addScor){
				TexaminationBarbecue texaminationBarbecue2 = texaminationBarbecueService.get(questionId);
				entity.setScore(texaminationBarbecue2.getQuestionScore());
			}else{
				entity.setScore(new BigDecimal(0));
			}
		}else{
			entity.setScore(new BigDecimal(0));
		}
		String id = entity.getId();
		if(StringUtils.isNotBlank(id)){
			texaminationBarbecueRecordService.update(entity);
		}else{
			texaminationBarbecueRecordService.save(entity);
		}
		JSONObject parseObject = JSONObject.parseObject(JsonMapper.toJson(entity));
		AppController.renderSuccessObject(response, parseObject, MessageCode.SUCCESS_ACK);
	}
	
}
